﻿using AutoMapper;
using Common.Domain;
using Common.Infrastructure.Repository;
using Common.Services;
using MediatR;

namespace Business.WriteAPI.Application.Images
{
    public class ImageDelRequestHandler : IRequestHandler<ImageDelRequestModel, APIResult>
    {
        private readonly IBaseRepository<Image> imageRepository;
        private readonly IMapper mapper;
        public ImageDelRequestHandler(IBaseRepository<Image> imageRepository, IMapper mapper)
        {
            this.imageRepository = imageRepository;
            this.mapper = mapper;
        }
        public Task<APIResult> Handle(ImageDelRequestModel request, CancellationToken cancellationToken)
        {
            APIResult result = new APIResult();
            result.Code = EnumCode.DelSuccess;
            result.Msg = "删除成功";

            var entity = imageRepository.GetAll().Where(x=>x.Url == request.Url).FirstOrDefault();
            if(entity!=null)
            {
                entity.IsDel = true;
                imageRepository.Update(entity);
            }

            return Task.FromResult(result);
        }
    }
    public class ImageDelRequestModel:IRequest<APIResult>
    {
        public string Url { get; set; }
    }
}
